package com.agri.controller;

import com.agri.dto.OrderCreateDTO;
import com.agri.result.Result;
import com.agri.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 订单控制器
 * 提供订单创建、查询、支付等接口
 *
 * @author Agri-Fin-Sale Team
 * @since 2025-11-05
 */
@RestController
@RequestMapping("/order")
@PreAuthorize("hasRole('USER')")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     */
    @PostMapping
    public Result<?> createOrder(@Valid @RequestBody OrderCreateDTO orderCreateDTO) {
        return orderService.createOrder(orderCreateDTO);
    }

    /**
     * 根据ID获取订单详情
     */
    @GetMapping("/{orderId}")
    public Result<?> getOrderById(@PathVariable Long orderId) {
        return orderService.getOrderById(orderId);
    }

    /**
     * 获取用户订单列表
     */
    @GetMapping("/list")
    public Result<?> getUserOrders(@RequestParam Long userId,
                                  @RequestParam(required = false) Integer status) {
        return orderService.getUserOrders(userId, status);
    }

    /**
     * 取消订单
     */
    @PutMapping("/{orderId}/cancel")
    public Result<?> cancelOrder(@PathVariable Long orderId) {
        return orderService.cancelOrder(orderId);
    }

    /**
     * 支付订单
     */
    @PostMapping("/{orderId}/pay")
    public Result<?> payOrder(@PathVariable Long orderId) {
        return orderService.payOrder(orderId);
    }

    /**
     * 确认收货
     */
    @PutMapping("/{orderId}/confirm")
    public Result<?> confirmOrder(@PathVariable Long orderId) {
        return orderService.confirmOrder(orderId);
    }

    /**
     * 删除订单
     */
    @DeleteMapping("/{orderId}")
    public Result<?> deleteOrder(@PathVariable Long orderId) {
        return orderService.deleteOrder(orderId);
    }

    /**
     * 获取订单统计信息
     */
    @GetMapping("/stats")
    public Result<?> getOrderStats(@RequestParam Long userId) {
        return orderService.getOrderStats(userId);
    }
}